package server

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"month_zh5/common/model/es"
	"month_zh5/common/model/mysql"
	"month_zh5/common/model/redis"
	"month_zh5/common/proto/goods"
	"strconv"
	"time"
)

func PageViewsTop(in *goods.PageViewsTopRequest) (*goods.PageViewsTopResponse, error) {
	views, err := mysql.GoodsViews()
	if err != nil {
		return nil, err
	}
	var good []*goods.Good
	for _, v := range views {
		good = append(good, &goods.Good{
			GoodsName:      v.GoodsName,
			GoodsInventory: v.GoodsInventory,
			GoodsViews:     v.GoodsViews,
		})
	}

	return &goods.PageViewsTopResponse{
		Good: good,
	}, nil
}

func HighlightSearch(in *goods.HighlightSearchRequest) (*goods.HighlightSearchResponse, error) {
	search, err := es.HSearch(in.GoodsName)
	if err != nil {
		return nil, err
	}
	var good []*goods.Good
	fmt.Println(&search, in)
	for _, v := range search.Hits.Hits {
		good = append(good, &goods.Good{
			GoodsName:      v.Source.GoodsName,
			GoodsPrice:     strconv.Itoa(int(v.Source.GoodsPrice)),
			GoodsInventory: int64(v.Source.GoodsInventory),
			GoodsViews:     int64(v.Source.GoodsViews),
		})
	}
	records := mysql.WhereForRecords(int(in.Userid))
	err = mysql.CreateForRecords(mysql.SearchForRecords{
		UserID:           in.Userid,
		SearchForRecords: in.GoodsName,
		Num:              records.Num + 1,
	})
	if err != nil {
		return nil, err
	}
	return &goods.HighlightSearchResponse{
		Good: good,
	}, nil
}

func Search(in *goods.SearchRequest) (*goods.SearchResponse, error) {
	search, err := es.Search(in.GoodsName)
	if err != nil {
		return nil, err
	}
	var good []*goods.Good
	for _, v := range search.Hits.Hits {
		good = append(good, &goods.Good{
			GoodsName:      v.Source.GoodsName,
			GoodsPrice:     strconv.Itoa(int(v.Source.GoodsPrice)),
			GoodsInventory: int64(v.Source.GoodsInventory),
			GoodsViews:     int64(v.Source.GoodsViews),
		})
	}
	records := mysql.WhereForRecords(int(in.Userid))
	err = mysql.CreateForRecords(mysql.SearchForRecords{
		UserID:           in.Userid,
		SearchForRecords: in.GoodsName,
		Num:              records.Num + 1,
	})
	if err != nil {
		return nil, err
	}
	return &goods.SearchResponse{
		Good: good,
	}, err
}

func SearchForRecords(in *goods.SearchForRecordsRequest) (*goods.SearchForRecordsResponse, error) {
	records := mysql.WhereForRecordsTop(int(in.Userid))
	var str []string
	for _, v := range records {
		str = append(str, v.SearchForRecords)
	}
	return &goods.SearchForRecordsResponse{
		SearchForRecords: str,
	}, nil
}

func ShoppingCart(in *goods.ShoppingCartRequest) (*goods.ShoppingCartResponse, error) {
	whereGoods := mysql.WhereGoods(int(in.GoodsID))
	err := mysql.AddShoppingCart(mysql.ShoppingCart{
		Model:          gorm.Model{},
		UserID:         in.Userid,
		GoodsId:        in.GoodsID,
		GoodsName:      in.GetGoodsName(),
		GoodsPrice:     float64(whereGoods.ID),
		GoodsInventory: in.Num,
	})
	if err != nil {
		return nil, err
	}
	return &goods.ShoppingCartResponse{
		Date: "添加购物车成功",
	}, nil
}

func Comment(in *goods.CommentRequest) (*goods.CommentResponse, error) {
	where := mysql.Where(int(in.GoodsId))
	var comment []*goods.Comment
	for _, v := range where {
		comment = append(comment, &goods.Comment{
			UserID:  v.UserID,
			Comment: v.Comment,
		})
	}
	whereGoods := mysql.WhereGoods(int(in.GoodsId))
	good := &goods.Good{
		GoodsName:      whereGoods.GoodsName,
		GoodsPrice:     strconv.Itoa(int(whereGoods.GoodsPrice)),
		GoodsInventory: whereGoods.GoodsInventory,
		GoodsViews:     whereGoods.GoodsViews,
	}
	return &goods.CommentResponse{
		GoodsId: in.GoodsId,
		Good:    good,
		Comment: comment,
	}, nil
}

func CreateComment(in *goods.CreateCommentRequest) (*goods.CreateCommentResponse, error) {
	redis.Rdb.SetNX(context.Background(), "suo", "1", time.Minute*1)
	defer redis.Rdb.Del(context.Background(), "suo")

	err := mysql.CreateComments(mysql.Comments{
		GoodsId: in.GoodsId,
		UserID:  in.UserID,
		Comment: in.Comment,
	})
	if err != nil {
		return nil, err
	}
	return &goods.CreateCommentResponse{
		Date: "添加评论成功",
	}, nil
}
